package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.BizNews;
import com.ruoyi.system.mapper.BizCategoryMapper;
import com.ruoyi.system.domain.BizCategory;
import com.ruoyi.system.service.IBizCategoryService;

/**
 * 新闻分类Service业务层处理
 *
 * @author chensw
 * @date 2023-06-19
 */
@Service
public class BizCategoryServiceImpl implements IBizCategoryService
{
    @Autowired
    private BizCategoryMapper bizCategoryMapper;

    /**
     * 查询新闻分类
     *
     * @param categoryId 新闻分类主键
     * @return 新闻分类
     */
    @Override
    public BizCategory selectBizCategoryByCategoryId(Long categoryId)
    {
        return bizCategoryMapper.selectBizCategoryByCategoryId(categoryId);
    }

    /**
     * 查询新闻分类列表
     *
     * @param bizCategory 新闻分类
     * @return 新闻分类
     */
    @Override
    public List<BizCategory> selectBizCategoryList(BizCategory bizCategory)
    {
        return bizCategoryMapper.selectBizCategoryList(bizCategory);
    }

    /**
     * 新增新闻分类
     *
     * @param bizCategory 新闻分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizCategory(BizCategory bizCategory)
    {
        int rows = bizCategoryMapper.insertBizCategory(bizCategory);
        insertBizNews(bizCategory);
        return rows;
    }

    /**
     * 修改新闻分类
     *
     * @param bizCategory 新闻分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizCategory(BizCategory bizCategory)
    {
        bizCategoryMapper.deleteBizNewsByCategoryId(bizCategory.getCategoryId());
        insertBizNews(bizCategory);
        return bizCategoryMapper.updateBizCategory(bizCategory);
    }

    /**
     * 批量删除新闻分类
     *
     * @param categoryIds 需要删除的新闻分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizCategoryByCategoryIds(Long[] categoryIds)
    {
        bizCategoryMapper.deleteBizNewsByCategoryIds(categoryIds);
        return bizCategoryMapper.deleteBizCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除新闻分类信息
     *
     * @param categoryId 新闻分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizCategoryByCategoryId(Long categoryId)
    {
        bizCategoryMapper.deleteBizNewsByCategoryId(categoryId);
        return bizCategoryMapper.deleteBizCategoryByCategoryId(categoryId);
    }

    /**
     * 新增新闻内容信息
     *
     * @param bizCategory 新闻分类对象
     */
    public void insertBizNews(BizCategory bizCategory)
    {
        List<BizNews> bizNewsList = bizCategory.getBizNewsList();
        Long categoryId = bizCategory.getCategoryId();
        if (StringUtils.isNotNull(bizNewsList))
        {
            List<BizNews> list = new ArrayList<BizNews>();
            for (BizNews bizNews : bizNewsList)
            {
                bizNews.setCategoryId(categoryId);
                list.add(bizNews);
            }
            if (list.size() > 0)
            {
                bizCategoryMapper.batchBizNews(list);
            }
        }
    }
}
